
<template>
  <section>
    <!--工具条 - 高级查询框-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keywords" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onsale">批量上架</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="offsale">批量下架</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55" fixed>
      </el-table-column>
      <el-table-column type="index" fixed width="60">
      </el-table-column>
      <el-table-column prop="title" fixed label="名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="cover" label="封面图" width="120" sortable>
        <template slot-scope="scope">
          <img :src="scope.row.cover" width="30px" height="30px">
        </template>
      </el-table-column>
      <el-table-column prop="saleprice" label="原价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="costprice" label="折扣价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="isnew" label="是否新车" width="120" sortable>
        <template slot-scope="scope">
          <span v-if="scope.isnew == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="reigstertime" label="上牌时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="mileage" label="里程数" width="100" sortable>
      </el-table-column>
      <el-table-column prop="shopname" label="店铺名称" width="100" sortable>
      </el-table-column>
      <el-table-column prop="onsaletime" label="上架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="offsaletime" label="下架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="130" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1" style="color: blue">上架</span>
          <span v-else>下架</span>
        </template>
      </el-table-column>
      <el-table-column prop="costeffective" label="超值" min-width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.costeffective == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="rushsale" label="急售" min-width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.rushsale == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="quasinewcar" label="准新车" min-width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.quasinewcar == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="transitivecountry" label="可提全国" min-width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.transitivecountry == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="type.name" label="类型" width="100" sortable>
      </el-table-column>
      <el-table-column prop="seller.nickName" label="营销人员" width="100" sortable>
      </el-table-column>
      <el-table-column prop="auditstate" label="审核状态" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.auditstate == 1">已审核</span>
          <span v-else>未审核</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="danger" size="small" style="margin-top: 10px" v-if="scope.row.auditstate == 0" @click="handlePass(scope.$index, scope.row)">审核</el-button>
          <el-button type="danger" size="small" style="margin-top: 10px" v-if="scope.row.auditstate == 1 && scope.row.state== 0" @click="handleOnCar(scope.$index, scope.row)">上架</el-button>
          <el-button type="danger" size="small" style="margin-top: 10px" v-if="scope.row.state== 1" @click="handleOffCar(scope.$index, scope.row)">下架</el-button>
          <br>
          <el-button type="danger" size="small" style="margin-top: 10px" @click="handleResource(scope.$index, scope.row)">车辆维护</el-button>

          <el-button type="danger" size="small" style="margin-top: 10px" @click="handleProtect(scope.$index, scope.row)">上传保险</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条 - 批量删除&分页条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="prev, pager, next"
          :current-page="query.currentPage"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :total="pageInfo.total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--
    新增/修改界面
    -->
    <el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="carForm" label-width="80px"  ref="carForm">
        <el-form-item label="标题">
          <el-input v-model="carForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload"

              :on-remove="coverHandleRemove"
              :on-success="coverHandleSuccess"
              :file-list="coverFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="原价">
          <el-input v-model="carForm.saleprice" type="number" style="width:220px" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价">
          <el-input v-model="carForm.costprice" type="number" style="width:220px" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否新车">
          <el-radio-group v-model="carForm.isnew">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上牌时间">
          <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="carForm.reigstertime"
              type="date"
              placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="里程数">
          <el-input v-model="carForm.mileage" type="number" style="width:220px" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="超值">
          <el-radio-group v-model="carForm.costeffective">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="急售">
          <el-radio-group v-model="carForm.rushsale">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="准新车">
          <el-radio-group v-model="carForm.quasinewcar">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可迁全国">
          <el-radio-group v-model="carForm.transitivecountry">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆类型">
          <el-cascader v-model="carForm.type.id"
                       :options="carTypeTree"
                       :props="{
							   checkStrictly: true,
							   label:'name',
							   value:'id'
							}"
                       clearable></el-cascader>
        </el-form-item>

        <el-form-item label="基本信息">
          <quill-editor   v-model="carForm.carDetail.info" :options="quillOption">
          </quill-editor>
        </el-form-item>


        <el-form-item label="销售员">
          <template>
            <el-select v-model="carForm.seller" clearable value-key="id" placeholder="请选择" v-on:change="getEmplShopInfo">
              <el-option
                  v-for="item in employees"
                  :key="item.id"
                  :label="item.nickName"
                  :value="item">
              </el-option>
            </el-select>
          </template>

            <el-form-item label="关联店铺">
              <el-select v-model="carForm.shop.id"  placeholder="请选择">
                <el-option
                    v-for="item in shops"
                    :key="item.id"
                    :label="item.name"
                    :value="item.shop_id">
                </el-option>
              </el-select>
            </el-form-item>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


    <el-dialog title="资源维护" :visible.sync="carResourceVisible" :close-on-click-modal="false">
      <el-form :model="carResource" label-width="80px" ref="carResourceForm">
        <el-form-item label="主图">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload"
              :on-remove="imgHandleRemove"
              :on-success="imgHandleSuccess"
              :file-list="imgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="副图">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload"
              :on-remove="viceHandleRemove"
              :on-success="viceHandleSuccess"
              :file-list="viceFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="更多图">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload"
              :on-remove="moreHandleRemove"
              :on-success="moreHandleSuccess"
              :file-list="moreFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carResourceVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveCarResource" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

<!--上传保修说明-->
    <el-dialog title="上传保险" :visible.sync="carProtectVisible" :close-on-click-modal="false">
      <el-form :model="carDetail" label-width="80px" ref="carResourceForm">
        <el-form-item label="标题">
          <el-input v-model="carDetail.cartitle" disabled auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="检查人">
          <template>
            <el-select v-model="carDetail.detector" clearable value-key="id" placeholder="请选择">
              <el-option
                  v-for="item in employees"
                  :key="item.id"
                  :label="item.nickName"
                  :value="item">
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="检修时间">
          <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="carDetail.detectiontime"
              type="date"
              placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>


        <el-form-item label="意外保险单">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload"
              :on-remove="accidentHandleRemove"
              :on-success="accidentHandleSuccess"
              :file-list="accidentFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="核心部件检测">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload"
              :on-remove="coreComponentsHandleRemove"
              :on-success="coreComponentsHandleSuccess"
              :file-list="coreComponentsFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>


        <el-form-item label="常用功能检测">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload"
              :on-remove="functionsHandleRemove"
              :on-success="functionsHandleSuccess"
              :file-list="functionsFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="外观内饰检测">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload"
              :on-remove="appearanceHandleRemove"
              :on-success="appearanceHandleSuccess"
              :file-list="appearanceFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carProtectVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveCarDetailResource" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

<!--   人工审核 -->
    <el-dialog title="人工审核" :visible.sync="saveDateVisible" :close-on-click-modal="false">
      <el-form :model="savaForm" label-width="80px" :rules="savaFormRules" ref="savaForm">
        <el-form-item label="店铺名称" >
          <el-input v-model="savaForm.title" disabled auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="驳回信息" >

          <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="savaForm.content">
          </el-input>

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="returnInfoMsgTest">驳回</el-button>
        <el-button type="primary" @click.native="successInfoMsg" :loading="addLoading">审批通过</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import { quillEditor } from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import quillConfig from "../../common/js/quill-config.js"
 //调用编辑器
export default {
  components:{
    quillEditor
  },

  data() {
    return {
      saveDateVisible:false,
      quillOption:quillConfig,
      filters: {
        name: ''
      },
      users: [],
      total: 0,
      page: 1,
      listLoading: false,
      sels: [],//列表选中列
      checkAll:false,
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      saveCarDate: false,//新增界面是否显示
      addLoading: false,
      savaFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入标识', trigger: 'blur' }
        ]
      },
      //新增修改界面数据
      carForm: {
        id:null,
        title:"",
        cover:"",
        saleprice:0,
        costprice:0,
        isnew:0,
        reigstertime:"",
        mileage:null,
        costeffective:0,
        rushsale:0,
        quasinewcar:0,
        transitivecountry:0,
        type:{
          id:null,
          name:""
        },
        typeId:null,
        carDetail:{
          info:""
        },
        seller:{
          id:null,
          nickName:''
        },
        shop:{
          id:null
        }

      },
      shops:[],
      saveFormVisible:false,
      pageInfo:{
        rows:[],
        total:0
      },
      query:{
        pageSize:10,
        currentPage:1,
        keywords:''
      },
      coverFileList:[],
      carTypeTree:[],
      employees:[],
      //资源维护
      carResourceVisible:false,
      carResource:{
        carId:null,
        img:'',
        viceimg:'',
        moreimg:''

      },
      imgFileList:[],

      //附图
      viceFileList:[],

      //更多图
      moreFileList:[],

      carProtectVisible:false,
      carDetail:{
        carId:null,
        accidentInvestigation:'',
        detector:'',
        detectiontime:null,
        cartitle:'',
        coreComponentsInvestigation:'',
        commonFunctionsInvestigation:'',
        appearanceInspection:''
      },
      //意外图
      accidentFileList:[],
      //核心部件
      coreComponentsFileList:[],
      //功能检查
      functionsFileList:[],

      appearanceFileList:[],
      //人工审核
      savaForm:{
        carId:null,
        content:'',
        title:''

      }


    }
  },
  methods: {
    // handleOffCar单个下架
    handleOffCar(index,row){
      var ids=[]
      ids.push(row.id);
      this.$confirm('确认下架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/offsale',ids).then((res) => {
          this.listLoading = false;
          res=res.data
          if(res.success){
            this.$message({
              message: '下架成功',
              type: 'success'
            });
          }else{
            this.$message({
              message:"下架失败",
              type: 'error'
            });
          }
          this.getCars()
        });
      }).catch(() => {

      });
    },
    //单个上架
    handleOnCar(index,row){
      console.log(row.id);
      var ids=[]
      ids.push(row.id);

      this.$confirm('确认上架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {


        this.listLoading = true;
        this.$http.post('/car/onsale',ids).then((res) => {
          this.listLoading = false;
          res=res.data
          if(res.success){
            this.$message({
              message: '上架成功'+res.message,
              type: 'success'
            });
          }else{
            this.$message({
              message: res.message,
              type: 'error'
            });
          }
          this.getCars()
        });
      }).catch(() => {

      });

    },


    //上架
    onsale(){
      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾上架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认上架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/onsale',ids).then((res) => {
          this.listLoading = false;
            res=res.data
          if(res.success){
            this.$message({
              message: '上架成功'+res.message,
              type: 'success'
            });
          }else{
            this.$message({
              message: res.message,
              type: 'error'
            });
          }
          this.getCars()
        });
      }).catch(() => {

      });
    },
    //下架
    offsale(){

      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾下架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认下架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/offsale',ids).then((res) => {
          this.listLoading = false;
            res=res.data
          if(res.success){
            this.$message({
              message: '下架成功',
              type: 'success'
            });
          }else{
            this.$message({
              message:"下架失败",
              type: 'error'
            });
          }
          this.getCars()
        });
      }).catch(() => {

      });
    },

    //人工审核
    handlePass(index,row){
      this.saveDateVisible=true
      this.savaForm.title=row.title
      this.savaForm.carId=row.id
    },

    //通过
    successInfoMsg(){
      this.$http.post("/car/successInfoMsg",this.savaForm)
          .then(result=>{
            result=result.data;
            if(result.success){
              this.$message({
                message: '审核成功',
                type: 'success'
              });
            }
            this.saveDateVisible=false;
            this.flushDate()
          })
    },

    returnInfoMsgTest(){
      console.log(this.savaForm)
      this.$http.post("/car/returnMsg",this.savaForm)
          .then(result=>{
            result=result.data;
            if(result.success){
              this.$message({
                message: '驳回成功',
                type: 'success'
              });
            }
            this.saveDateVisible=false;
            this.flushDate()
          })

    },


    // 外观内饰检测
    appearanceHandleRemove(file, fileList){
      this.carDetail.appearanceInspection=""
      var path=file.response.object

      this.$http.delete("/fastDfs/delete?path="+path)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              if(fileList && fileList.length > 0) {
                for (var i = 0; i < fileList.length; i++) {
                  var url = fileList[i].response ? fileList[i].response.object : fileList[i].url;
                  this.carDetail.appearanceInspection += url + ",";
                }
                if (this.carDetail.appearanceInspection.endsWith(",")) {
                  this.carDetail.appearanceInspection = this.carDetail.appearanceInspection.substring(0, this.carDetail.appearanceInspection.length - 1);
                }
              }
            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    appearanceHandleSuccess(response, file, fileList){
      this.carDetail.appearanceInspection=""
      for (let i = 0; i <fileList.length ; i++) {
        var appearanceInspection=fileList[i].response.object

        if(this.carDetail.appearanceInspection){
          this.carDetail.appearanceInspection=this.carDetail.appearanceInspection+","+appearanceInspection
        }else {
          this.carDetail.appearanceInspection=appearanceInspection;
        }

      }

    },


    // 常用功能检测
    functionsHandleRemove(file, fileList){
      this.carDetail.commonFunctionsInvestigation=""
      var path=file.response.object

      this.$http.delete("/fastDfs/delete?path="+path)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              if(fileList && fileList.length > 0) {
                for (var i = 0; i < fileList.length; i++) {
                  var url = fileList[i].response ? fileList[i].response.object : fileList[i].url;
                  this.carDetail.commonFunctionsInvestigation += url + ",";
                }
                if (this.carDetail.commonFunctionsInvestigation.endsWith(",")) {
                  this.carDetail.commonFunctionsInvestigation = this.carDetail.commonFunctionsInvestigation.substring(0, this.carDetail.commonFunctionsInvestigation.length - 1);
                }
              }



            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    functionsHandleSuccess(response, file, fileList){
      this.carDetail.commonFunctionsInvestigation=""
      for (let i = 0; i <fileList.length ; i++) {
        var commonFunctionsInvestigation=fileList[i].response.object

        if(this.carDetail.commonFunctionsInvestigation){
          this.carDetail.commonFunctionsInvestigation=this.carDetail.commonFunctionsInvestigation+","+commonFunctionsInvestigation
        }else {
          this.carDetail.commonFunctionsInvestigation=commonFunctionsInvestigation;
        }

      }

    },

    // 核心保险图
    coreComponentsHandleRemove(file, fileList){
      this.carDetail.coreComponentsInvestigation=""
      var path=file.response.object

      this.$http.delete("/fastDfs/delete?path="+path)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              if(fileList && fileList.length > 0) {
                for (var i = 0; i < fileList.length; i++) {
                  var url = fileList[i].response ? fileList[i].response.object : fileList[i].url;
                  this.carDetail.coreComponentsInvestigation += url + ",";
                }
                if (this.carDetail.coreComponentsInvestigation.endsWith(",")) {
                  this.carDetail.coreComponentsInvestigation = this.carDetail.coreComponentsInvestigation.substring(0, this.carDetail.coreComponentsInvestigation.length - 1);
                }
              }



            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    coreComponentsHandleSuccess(response, file, fileList){
      this.carDetail.coreComponentsInvestigation=""
      for (let i = 0; i <fileList.length ; i++) {
        var coreComponentsInvestigation=fileList[i].response.object

        if(this.carDetail.coreComponentsInvestigation){
          this.carDetail.coreComponentsInvestigation=this.carDetail.coreComponentsInvestigation+","+coreComponentsInvestigation
        }else {
          this.carDetail.coreComponentsInvestigation=coreComponentsInvestigation;
        }

      }

    },

//保存上传保险
    saveCarDetailResource(){


      this.$http.put("/carDetail",this.carDetail).then(result=>{
        result = result.data;
        if(result.success){
          this.$message({
            message: '上传成功',
            type: 'success'
          });
        }else {
          this.$message({
            message: '上传失败'+result.message,
            type: 'error'
          });
        }
        this.carProtectVisible=false;
      });

    },

    // 意外保险图
    accidentHandleRemove(file, fileList){
      this.carDetail.accidentInvestigation=""
      var path=file.response.object

      this.$http.delete("/fastDfs/delete?path="+path)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              if(fileList && fileList.length > 0) {
                for (var i = 0; i < fileList.length; i++) {
                  var url = fileList[i].response ? fileList[i].response.object : fileList[i].url;
                  this.carDetail.accidentInvestigation += url + ",";
                }
                if (this.carDetail.accidentInvestigation.endsWith(",")) {
                  this.carDetail.accidentInvestigation = this.carDetail.accidentInvestigation.substring(0, this.carDetail.accidentInvestigation.length - 1);
                }
              }



            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    accidentHandleSuccess(response, file, fileList){
      this.carDetail.accidentInvestigation=""
      for (let i = 0; i <fileList.length ; i++) {
        var accidentInvestigation=fileList[i].response.object

        if(this.carDetail.accidentInvestigation){
          this.carDetail.accidentInvestigation=this.carDetail.accidentInvestigation+","+accidentInvestigation
        }else {
          this.carDetail.accidentInvestigation=accidentInvestigation;
        }

      }

    },

    //上传保险
    handleProtect(index,row){
      //意外图
      this.accidentFileList=[],
          //核心部件
          this.coreComponentsFileList=[],
          //功能检查
          this.functionsFileList=[],

          this.appearanceFileList=[]

      this.carDetail=row
      this.carProtectVisible=true
      this.carDetail.carId=row.id
      this.getEmployees()
      this.carDetail.cartitle=row.title
      this.getReturnProtectCar(row.id)
    },
    //回显上传保修
    getReturnProtectCar(carId){

      this.$http.get("/carDetail/carId/"+carId)
          .then(result=> {
            result = result.data;
            if (result.success) {
              console.log(result.object)
              this.carDetail.detector=result.object.detector

              this.carDetail.detectiontime=result.object.detectiontime

              if (result.object.accidentInvestigation) {
                this.carDetail.accidentInvestigation=result.object.accidentInvestigation
                var imgs = result.object.accidentInvestigation.split(",");
                for (let i = 0; i < imgs.length; i++) {

                  let imgUrl = imgs[i]
                  let name = imgUrl.substring(imgUrl.lastIndexOf("/") + 1);
                  this.accidentFileList.push(
                      {
                        name: name, url: imgUrl, response: {object: imgUrl}
                      });
                }
              }
              if (result.object.coreComponentsInvestigation) {
                this.carDetail.coreComponentsInvestigation=result.object.coreComponentsInvestigation
                var imgs = result.object.coreComponentsInvestigation.split(",");
                for (let i = 0; i < imgs.length; i++) {

                  let imgUrl = imgs[i]
                  let name = imgUrl.substring(imgUrl.lastIndexOf("/") + 1);
                  this.coreComponentsFileList.push(
                      {
                        name: name, url: imgUrl, response: {object: imgUrl}
                      });
                }
              }

              if (result.object.commonFunctionsInvestigation) {
                this.carDetail.commonFunctionsInvestigation=result.object.commonFunctionsInvestigation
                var imgs = result.object.commonFunctionsInvestigation.split(",");
                for (let i = 0; i < imgs.length; i++) {

                  let imgUrl = imgs[i]
                  let name = imgUrl.substring(imgUrl.lastIndexOf("/") + 1);
                  this.functionsFileList.push(
                      {
                        name: name, url: imgUrl, response: {object: imgUrl}
                      });
                }
              }

              if (result.object.appearanceInspection) {
                this.carDetail.appearanceInspection=result.object.appearanceInspection
                var imgs = result.object.appearanceInspection.split(",");
                for (let i = 0; i < imgs.length; i++) {

                  let imgUrl = imgs[i]
                  let name = imgUrl.substring(imgUrl.lastIndexOf("/") + 1);
                  this.appearanceFileList.push(
                      {
                        name: name, url: imgUrl, response: {object: imgUrl}
                      });
                }
              }




            }



          })

    },

    //回显上传资源
    getReturnCarResource(carId){
      this.$http.get("/carResources/carId/"+carId)
          .then(result=>{
            result=result.data;
            if(result.success){
              if(result.object){
                if(result.object.img){
                  this.carResource.img=result.object.img
                  var imgs = result.object.img.split(",");
                  for (let i = 0; i <imgs.length ; i++) {

                    let imgUrl=imgs[i]
                    let name=imgUrl.substring(imgUrl.lastIndexOf("/")+1);
                    this.imgFileList.push(
                    {name:name,url:imgUrl,response:{object:imgUrl}
                    });
                  }
        }//附图
                if(result.object.viceimg){
                  this.carResource.viceimg=result.object.viceimg
                  var imgs = result.object.viceimg.split(",");
                  for (let i = 0; i <imgs.length ; i++) {

                    let imgUrl=imgs[i]
                    let name=imgUrl.substring(imgUrl.lastIndexOf("/")+1);
                    this.viceFileList.push(
                        {name:name,url:imgUrl,response:{object:imgUrl}
                        });
                  }
                }

                //更多图
                if(result.object.moreimg){
                  this.carResource.moreimg=result.object.moreimg
                  var imgs = result.object.moreimg.split(",");
                  for (let i = 0; i <imgs.length ; i++) {

                    let imgUrl=imgs[i]
                    let name=imgUrl.substring(imgUrl.lastIndexOf("/")+1);
                    this.moreFileList.push(
                        {name:name,url:imgUrl,response:{object:imgUrl}
                        });
                  }
                }


              }


            }
          })
    },

    //保存上传资源
    saveCarResource(){
      console.log(this.carResource)


          this.$http.put("/carResources",this.carResource)
                .then(result =>{

                  result=result.data
                  if(result.success){
                    this.$message({
                      message: '保存成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '保存失败'+result.message,
                      type: 'error'
                    });
                  }
                  //刷新页面
                  this.carResourceVisible=false;
                  this.search();
                })

    },
    handleResource(index,row){
      this.imgFileList=[]
      this.viceFileList=[]
      this.moreFileList=[]
      this.carResourceVisible=true;
      this.carResource.carId=row.id
      this.getReturnCarResource(row.id)
    },

    // 更多图
    moreHandleRemove(file, fileList){
      this.carResource.moreimg=""
      var path=file.response.object

      this.$http.delete("/fastDfs/delete?path="+path)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              if(fileList && fileList.length > 0) {
                for (var i = 0; i < fileList.length; i++) {
                  var url = fileList[i].response ? fileList[i].response.object : fileList[i].url;
                  this.carResource.moreimg += url + ",";
                }
                if (this.carResource.moreimg.endsWith(",")) {
                  this.carResource.moreimg = this.carResource.moreimg.substring(0, this.carResource.moreimg.length - 1);
                }
              }



            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    moreHandleSuccess(response, file, fileList){
      this.carResource.moreimg=""
      for (let i = 0; i <fileList.length ; i++) {

        var moreimg=fileList[i].response.object

        if(this.carResource.moreimg){
          this.carResource.moreimg=this.carResource.moreimg+","+moreimg
        }else {
          this.carResource.moreimg=moreimg;
        }

      }

    },


    // 副图
    viceHandleRemove(file, fileList){
      this.carResource.viceimg=""
      var path=file.response.object

      this.$http.delete("/fastDfs/delete?path="+path)
          .then(result => {
            result = result.data;
            if(result.success){
              console.log("当前存在shuju")
              console.log(fileList)
              this.$message({message: '删除成功!', type: 'success'});
              if(fileList && fileList.length > 0) {
                for (var i = 0; i < fileList.length; i++) {
                  var url = fileList[i].response.object;
                  this.carResource.viceimg += url + ",";
                }
                if (this.carResource.viceimg.endsWith(",")) {
                  this.carResource.viceimg = this.carResource.viceimg.substring(0, this.carResource.viceimg.length - 1);
                }

              }



            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    viceHandleSuccess(response, file, fileList){
      this.carResource.viceimg=""
      for (let i = 0; i <fileList.length ; i++) {
        var viceimg=fileList[i].response.object

        if(this.carResource.viceimg){
          this.carResource.viceimg=this.carResource.viceimg+","+viceimg
        }else {
          this.carResource.viceimg=viceimg;
        }

      }


    },

    // 主图
    imgHandleRemove(file, fileList){
       this.carResource.img=""
      var path=file.response.object

      this.$http.delete("/fastDfs/delete?path="+path)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              if(fileList && fileList.length > 0) {
                for (var i = 0; i < fileList.length; i++) {
                  var url = fileList[i].response.object;
                  this.carResource.img += url + ",";
                }
                if (this.carResource.img.endsWith(",")) {
                  this.carResource.img = this.carResource.img.substring(0, this.carResource.img.length - 1);
                }
              }



            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    imgHandleSuccess(response, file, fileList){
      this.carResource.img=""
      for (let i = 0; i <fileList.length ; i++) {
       var img=fileList[i].response.object
        this.carResource.img +=img+","
      }
      if(this.carResource.img.endsWith(",")){
        this.carResource.img=  this.carResource.img.substring( 0,this.carResource.img.length-1);
      }



    },

    //选择相应销售员，查询对应店铺信息
    getEmplShopInfo(val){
      this.carForm.shop.id=null

      let id=val.id
      this.$http.get("/car/employeeId/"+id)
                .then(res=>{
                  res=res.data;
                  if(res.success){
                    console.log(res.object)
                    this.shops=res.object

                  }
                })

    },


    coverHandleRemove(file, fileList){
      var path=file.response.object
      this.$http.delete("/fastDfs/delete?path="+path)
          .then(result => {
                result = result.data;
                if (result.success) {
                  this.$message({message: '删除成功!', type: 'success'});
                  this.carForm.cover=""
                  }

              })

          },

    coverHandleSuccess(response, file, fileList){

      this.carForm.cover = response.object;

    },

    //获取用户列表
    getEmployees() {
      this.$http.get("/employee")
          .then(res => {
            res = res.data

            if (res.success) {
              this.employees = res.object;
            }
          })
          .catch(err => {
            this.$message({
              message: '分页查询失败' + err.message,
              type: 'error'
            });
          })
    },

    getTreeData(){
      // 发送一个异步请求: get请求 /product/carType/treeData
      this.$http.get("/carType/tree").then(res=>{
        this.carTypeTree = res.data.object;

      });
    },
    //页面刷新
    flushDate(){
      this.getCars()
    },

    search(){
      this.query.currentPage=1;
      this.getCars();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getCars();
    },
    //获取用户列表
    getCars() {
      this.$http.post("/car",this.query)
          .then(result=>{
            result=result.data;

            if(result.success){
              console.log(result.object)
              this.pageInfo=result.object;


            }
          })

    },
    //删除
    handleDel: function (index, row) {
      var ids=[]
      ids.push(row.id)

      this.$confirm('确认删除选中记录吗？,这样可能会影响用户搜索该商品信息', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/car",ids)
            .then(result =>{
              this.listLoading=false;
              result=result.data
              if(result.success){
                this.$message({
                  message: '批量删除成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '批量删除失败'+result.message,
                  type: 'error'
                });
              }
              //刷新页面
              this.search();
            })


      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.coverFileList=[]
      this.saveFormVisible = true;


      this.carForm = Object.assign({}, row);
      this.carForm.shop={
        id:row.shopId,
      }
      var name=row.cover.substring(row.cover.lastIndexOf("/")+1);
      this.coverFileList.push(

          {
            name: name, url: row.cover, response: {object: row.cover}
          }
      );


      if(!this.carForm.carDetail){
        this.carForm.carDetail={
          info:''
        }
      }
      // console.log(row)
      //
      // this.carForm.type=[24,27]
      this.getTreeData()
      this.getEmployees()
    },
    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.savaForm ={
        id:null,
            title:"",
            cover:"",
            saleprice:0,
            costprice:0,
            isnew:0,
            reigstertime:"",
            mileage:null,
            costeffective:0,
            rushsale:0,
            quasinewcar:0,
            transitivecountry:0,
            type:{
          id:null,
              name:""
        },
        seller:{
          id:null,
          nickName: ''
        },
        shop:{
          id:null
        }

      }

      this.getTreeData()
      this.getEmployees()
    },
    //新增
    addSubmit: function () {



      this.$refs.carForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            var param=this.carForm
            if(param.type.id && param.type.id.length >0){
              param.type={id:param.type.id[param.type.id.length-1]}
            }
            this.$http.put("/car",this.carForm)
                .then(result =>{
                  this.listLoading=false;
                  result=result.data
                  if(result.success){
                    this.$message({
                      message: '保存成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '保存失败'+result.message,
                      type: 'error'
                    });
                  }
                  //刷新页面
                  this.saveFormVisible=false;
                  this.search();
                })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？,这样可能会影响用户搜索该商品信息', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;

        this.$http.patch("/car",ids)
            .then(result =>{
              this.listLoading=false;

              result=result.data
              if(result.success){
                this.$message({
                  message: '批量删除成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '批量删除失败'+result.message,
                  type: 'error'
                });
              }
              //刷新页面
              this.search();
            })


      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getCars();
    // this.getAllMenuName()
  }
}

</script>

<style scoped>

</style>